package chess.parsers;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.StringTokenizer;

import chess.database.DBConnection;
import chess.database.JavaDBConnection;
import chess.model.ChessBoardPos;
import chess.model.Session;
import chess.server.NewLineEvent;
import chess.server.NewLineListener;
import chess.server.ServerConnection;
import chess.utils.Game;
import chess.utils.Player;
import chess.utils.Style12;

public class GeneralParser extends Thread {

	private ChessBoardPos positionsModel;

	private ServerConnection con;

	private DBConnection db;

	private Hashtable<Integer, Game> observeGames = new Hashtable<Integer, Game>();

	private String line;

	private int observing = 0;

	public GeneralParser(ChessBoardPos positionModel) {

		System.out.println("GENERAL PARSER");
		this.positionsModel = positionModel;
		db = JavaDBConnection.getInstance();
		this.start();

	}

	public void run() {
		try {
			sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("blalalffdsfdsfdsfdsfdsfsfs");
		Session session = Session.getInstance();
		if (session.isConnected()) {
			System.out.println("blalalffdsfdsfdsfdsfdsfsfs");
			con = session.getConnection();

			con.addNewLineListener(new NewLineListener() {
				@Override
				public void handleNewLineEvent(NewLineEvent e) {

					System.out.println("1");
					line = e.getLine();
					if (line != null) {
						if (line.length() > 3
								&& line.substring(0, 4).equals("Game")) {
							if (observeGames.get(Integer.valueOf(line
									.substring(5, 6))) == null) {
								observing++;
								Game g = parseGameLine(line);
								observeGames.put(g.getID(), g);
								System.out.println(g.printString());
							}
						}
						if (line.length() > 4
								&& line.substring(0, 5).equals("{Game")) {
							if (observeGames.get(Integer.valueOf(line
									.substring(6, 7))) != null) {
								Hashtable<String, String> r = parseResultLine(line);

								if (observeGames.get(Integer.valueOf(r
										.get("id"))) != null) {
									db.setGameResult(observeGames.get(
											Integer.valueOf(r.get("id")))
											.getDatabaseID(), r.get("result"));

									observeGames.remove(r.get("id"));
									observing--;
								}
							}
						}
						if (line.length() > 3
								&& line.substring(0, 4).equals("<12>")) {
							System.out.println("PARSER12:" + line);
							Style12 style12 = new Style12(line);
							positionsModel.setPositions(style12);
							if (observeGames.get(style12.getGameID()) != null) {
								db.insertMove(style12.getPositionsString(),
										observeGames.get(style12.getGameID())
												.getDatabaseID());
							}
						}
					}
				}
			});
		}
	}

	private Game parseGameLine(String line) {
		System.out.println("Game parsing");
		StringTokenizer parse = new StringTokenizer(line);
		List<String> gameLine = new ArrayList<String>();
		while (parse.hasMoreTokens()) {
			gameLine.add(parse.nextToken());
		}
		int gameID = Integer.valueOf(gameLine.get(1).substring(0,
				gameLine.get(1).length() - 1));
		System.out.println("PARSER: Game ID:" + gameID);

		String player1 = gameLine.get(2);
		String player2 = gameLine.get(4);
		int player1Rating = 0;
		int player2Rating = 0;
		// jestli je hrac hostem (rating "++++") nastavime mu rating 0
		if (!(gameLine.get(3).substring(1, gameLine.get(3).length() - 1))
				.equals("++++")) {
			player1Rating = Integer.valueOf(gameLine.get(3).substring(1,
					gameLine.get(3).length() - 1));
		}
		if (!(gameLine.get(5).substring(1, gameLine.get(5).length() - 1))
				.equals("++++")) {
			player2Rating = Integer.valueOf(gameLine.get(5).substring(1,
					gameLine.get(5).length() - 1));
		}

		Game exactGame = new Game(gameID);
		exactGame.setPlayer1(new Player(player1, player1Rating));
		exactGame.setPlayer2(new Player(player2, player2Rating));

		int player1ID;
		int player2ID;
		int databaseID;
		player1ID = db.insertPlayer(player1, player1Rating);
		player2ID = db.insertPlayer(player2, player2Rating);
		databaseID = db.insertGame(player1ID, player2ID);

		exactGame.setDatabaseID(databaseID);

		return exactGame;
	}

	private Hashtable<String, String> parseResultLine(String line) {
		System.out.println("2");
		Hashtable<String, String> result = new Hashtable<String, String>();
		System.out.println("2");
		StringTokenizer parse = new StringTokenizer(line);
		System.out.println("2");
		List<String> resultLine = new ArrayList<String>();
		System.out.println("2");
		while (parse.hasMoreTokens()) {
			resultLine.add(parse.nextToken());
		}
		System.out.println("2");

		result.put("id", resultLine.get(1));
		result.put("player1", resultLine.get(2).substring(1,
				resultLine.get(2).length()));
		result.put("player2", resultLine.get(4).substring(0));
		result.put("result", resultLine.get(resultLine.size() - 1));
		// result.put("resultPlayer1", resultLine.get(6).substring(0,
		// resultLine.get(6).length() - 1));
		// result.put("resultPlayer2", resultLine.get(2).substring(2));
		System.out.println("2");
		return result;
	}
}
